package org.jeecg.modules.wlhy.bigscreen;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressReceive;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressSend;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressReceiveService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressSendService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.jeecg.modules.wlhy.home.vo.ShippingMonthTotalVO;
import org.jeecg.modules.wlhy.hydriver.service.IHyDriverService;
import org.jeecg.modules.wlhy.hyvehicle.service.IHyVehicleService;
import org.jeecg.modules.wlhy.order.entity.OrderMonthTotalDTO;
import org.jeecg.modules.wlhy.order.service.IHyOrderService;
import org.jeecg.modules.wlhy.shipping.entity.ShippingMonthTotalDTO;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.tj.mapper.HyTjMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/kanban")
public class KanbanController {

    @Resource
    private HyTjMapper hyTjMapper;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Autowired
    private IHyOrderService hyOrderService;

    @Autowired private IHyCustomerService customerService;
    @Autowired private IHyDriverService driverService;
    @Autowired private IHyVehicleService vehicleService;
    @Autowired private IHyCustomerAddressSendService sendService;
    @Autowired private IHyCustomerAddressReceiveService receiveService;

    private QueryWrapper<HyCustomer> customerQueryWrapper;

    public KanbanController(){
        customerQueryWrapper = new QueryWrapper<>();
        customerQueryWrapper.eq("examine_status","2");

//        driverQueryWrapper = new QueryWrapper<>();
//        driverQueryWrapper.eq("","");
    }

    @GetMapping(value = "/query")
    public Result<JSONObject> query() {
        JSONObject jo = new JSONObject();
        String customerId = "all";
        Map<String,String> orderData = hyTjMapper.getHomeOrderTj(customerId);
        jo.put("order", orderData);
        Map<String,String> shippingData = hyTjMapper.getHomeShippingTj(customerId);
        jo.put("shipping", shippingData);

        Map<String,String> payData = hyTjMapper.getHomePayTj(customerId);
        jo.put("pay", payData);

        int customerCount = customerService.count(customerQueryWrapper);
        jo.put("customerCount",customerCount);
        int driverCount = driverService.count();
        jo.put("driverCount",driverCount);

        int vehicleCount = vehicleService.count();
        jo.put("vehicleCount",vehicleCount);

        //货主托运量排行，占比
        List<Map> customerph = hyTjMapper.getCustomerLoadingPH();
        jo.put("customerph",customerph);

        return Result.OK(jo);
    }

    @GetMapping(value = "/getMapData")
    public Result<?> getMapData() {
        List<HyCustomerAddressSend> addressSends = sendService.list(new QueryWrapper<HyCustomerAddressSend>().eq("iz_used", "1"));
        List<HyCustomerAddressReceive> addressReceive = receiveService.list(new QueryWrapper<HyCustomerAddressReceive>().eq("iz_used", "1"));

        List<Map> data = new ArrayList<>();
        for (HyCustomerAddressSend addressSend : addressSends) {
            Map item = new HashMap();
            item.put("name",addressSend.getAddressName());
            item.put("lon",addressSend.getLongitude());
            item.put("lat",addressSend.getLatitude());
            data.add(item);
        }
        for (HyCustomerAddressReceive hyCustomerAddressReceive : addressReceive) {
            Map item = new HashMap();
            item.put("name",hyCustomerAddressReceive.getAddressName());
            item.put("lon",hyCustomerAddressReceive.getLongitude());
            item.put("lat",hyCustomerAddressReceive.getLatitude());
            data.add(item);
        }
        return Result.OK(data);
    }

    @GetMapping(value = "/getMonthTotal")
    public Result<?> getMonthTotal() {
        String[]  latestMonth = DateUtils.getLatest12Month().split("=");
        String customerId = "all";

        List<ShippingMonthTotalDTO> shippingMonthTotalDTOS = hyShippingNoteService.getShippingMonthTotal(customerId , latestMonth[0] , latestMonth[1]);
        return Result.OK(shippingMonthTotalDTOS);
    }

    @GetMapping(value = "/getShippingAndOrderMonthTotal")
    public Result<?> getShippingAndOrderMonthTotal() {
        String[]  latestMonth = DateUtils.getLatest12Month().split("=");
        String customerId = "all";

        List<ShippingMonthTotalDTO> shippingMonthTotalDTOS = hyShippingNoteService.getShippingMonthTotal(customerId , latestMonth[0] , latestMonth[1]);
        List<OrderMonthTotalDTO>  orderMonthTotalDTOS = hyOrderService.getOrderMonthTotal(customerId ,latestMonth[0] , latestMonth[1]);

        Map<String, Integer> orderMonthTotalDTOMap = new HashMap<>();
        if(CollectionUtils.isNotEmpty(orderMonthTotalDTOS)){
            for (OrderMonthTotalDTO orderMonthTotalDTO : orderMonthTotalDTOS) {
                orderMonthTotalDTOMap.put(orderMonthTotalDTO.getMonths(), orderMonthTotalDTO.getOrderTotal());
            }
        }

        List<ShippingMonthTotalVO> shippingMonthTotalVOs = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(shippingMonthTotalDTOS)){
            for(ShippingMonthTotalDTO shippingMonthTotalDTO :shippingMonthTotalDTOS){

                ShippingMonthTotalVO shippingMonthTotalVO = new ShippingMonthTotalVO();
                shippingMonthTotalVO.setType(shippingMonthTotalDTO.getMonths()+"月");
                shippingMonthTotalVO.setMonths(shippingMonthTotalDTO.getMonths());
                shippingMonthTotalVO.setShippingTotal(shippingMonthTotalDTO.getShippingTotal());
                shippingMonthTotalVO.setOrderTotal(
                        MapUtils.isNotEmpty(orderMonthTotalDTOMap) &&
                                orderMonthTotalDTOMap.get(shippingMonthTotalDTO.getMonths()) != null ?
                                orderMonthTotalDTOMap.get(shippingMonthTotalDTO.getMonths())  : 0
                );
                shippingMonthTotalVOs.add(shippingMonthTotalVO) ;
            }
        }
        return Result.OK(shippingMonthTotalVOs);
    }

}
